package com.qk.manangement.controller;


import com.qk.common.response.PageResult;
import com.qk.common.response.Result;
import com.qk.entity.pojo.Role;
import com.qk.entity.request.role.AddRoleRequest;
import com.qk.manangement.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取角色列表
     */
    @GetMapping
    public Result list(String name, String label,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResult<Role> rolePageResult = roleService.getRoleList(name, label, page, pageSize);
        return Result.success(rolePageResult);
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean success = roleService.deleteRole(id);
        if (!success) {
            return Result.error("删除角色失败");
        }
        return Result.success();
    }

    /**
     * 添加角色
     */
    @PostMapping
    public Result add(@RequestBody AddRoleRequest  addRoleRequest) {
        boolean success = roleService.addRole(addRoleRequest);
        if (!success) {
            return Result.error("添加角色失败");
        }
        return Result.success();
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Role role = roleService.getById(id);
        if (role == null){
            return Result.error("角色不存在");
        }
        return Result.success(role);
    }

    /**
     * 修改角色信息
     */
    @PutMapping
    public Result update(@RequestBody Role role) {
        boolean success = roleService.updateRole(role);
        if (!success) {
            return Result.error("修改角色失败");
        }
        return Result.success();
    }

    /**
     * 查询所有用户列表
     */
    @GetMapping("/list")
    public Result list() {
        List<Role> roleList = roleService.list();
        return Result.success(roleList);
    }


}
